/**
    Copyright (c) 2010 yakiimo02
    Distributed under the New BSD License.
    See included license.txt or http://www.yakiimo3d.com/NewBSDLicense.txt
**/

/**
    LICENSE

    Copyright (c) 2006-2008 Kevin Beason (kevin.beason@gmail.com)

    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "DXUT.h"
#include "DXHelper.h"
#include "DataBufferDX11.h"
#include "optimized/SmallPtCPU_MT.h"
#include "optimized/SphereSOA.h"
#include "optimized/SmallPtTask.h"

namespace OPT
{

/**
*/
SmallPtCPU_MT::SmallPtCPU_MT() : m_buffer( NULL ), m_nWidth( 0 ), m_nHeight( 0 ), m_bTaskStart( false )
{
}

/**
*/
SmallPtCPU_MT::~SmallPtCPU_MT()
{
}

/**
*/
void SmallPtCPU_MT::Create( DataBufferDX11* pDataBuffer )
{
    const int nDataCount = pDataBuffer->GetCount();
    //m_buffer = new Vec[ nDataCount ];
    m_buffer = reinterpret_cast< Vec* >( _aligned_malloc( nDataCount*sizeof(Vec), 16 ) );
    for( int nIndex=0; nIndex<nDataCount; ++nIndex )
    {
        m_buffer[ nIndex ] = Vec( 0.0f, 0.0f, 0.0f );
    }

    InitSphereSOAs();

    m_tasks.Start();
    m_bTaskStart = true;
}

/**
*/
void SmallPtCPU_MT::Render( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, DataBufferDX11* pDataBuffer )
{    
    const unsigned int nWidth = static_cast< unsigned int >( pDataBuffer->GetWidth() );
    const unsigned int nHeight = static_cast< unsigned int >( pDataBuffer->GetHeight() );
        
    CWorkerThread* pThread = CWorkerThread::GetCurrent();
    CTaskCompletion	completion;
    RootTask rootTask( &completion );

    rootTask.SetPixelCnt( nWidth*nHeight );
    rootTask.SetpSmallPtCPU_MT( this );

    //m_cam = Ray(Vec(50.0f,52.0f,295.6f), Vec(0.0f,-0.042612f,-1.0f).norm());
    m_cam = Ray(Vec(50.0f,52.0f,295.6f), VectorNormalize( Vec(0.0f,-0.042612f,-1.0f) ));
    m_cx = Vec( nWidth*.5135f / nHeight );
    //m_cy = (m_cx%m_cam.d).norm()*.5135f;
    m_cy = VectorNormalize((m_cx%m_cam.d))*.5135f;

    m_nWidth = nWidth;
    m_nHeight = nHeight;

#if 0
    /* create one big root task, to avoid going idle during update+draw (spins more) */ 
    pThread->PushTask( &rootTask );
    pThread->WorkUntilDone( &completion );
#else
    /* don't use a big root task, go idle between phases (syncs more) */ 
    rootTask.Run( pThread );	
#endif

    D3DXVECTOR4 vPixel;

    // copy to DataBuffer's cpu buffer
    BufType* buffer = pDataBuffer->GetpData();
    for( unsigned int i=0; i<nWidth*nHeight; ++i ) 
    {
        buffer[ i ].x = m_buffer[ i ].x;
        buffer[ i ].y = m_buffer[ i ].y;
        buffer[ i ].z = m_buffer[ i ].z;
    }

    // copy from cpu to gpu
    pDataBuffer->UpdateBuffer( pd3dDevice, pd3dImmediateContext );
}

/**
*/
void SmallPtCPU_MT::Destroy()
{
    if( m_buffer ) {
        _aligned_free( m_buffer );
        m_buffer = NULL;
    }
    //SAFE_DELETE_ARRAY( m_buffer );
    if( m_bTaskStart ) {
        m_tasks.Stop();
        m_bTaskStart = false;
    }
}

}